package com.zhao.controller;

import com.zhao.pojo.Admin;
import com.zhao.pojo.OrderForm;
import com.zhao.pojo.SchoolCounsellor;
import com.zhao.pojo.Student;
import com.zhao.service.OrderFormService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

@Controller
public class OrderFormController {
    @Autowired
    private OrderFormService orderFormService;

    @RequestMapping("student-approvalOrder.do")
    public String studentApprovalOrder(String orderFormID,HttpSession session){
        Student student = (Student)session.getAttribute("student");
        if(student == null){
            return "redirect:/";
        }
        String res = orderFormService.studentApprovalOrder(student,orderFormID,session);
        System.out.println("res-studentApprovalOrder: "+res);
        if(!res.equals("success")){
            return "404";
        }
        return "redirect:/studentGetAllMySellOrders.do";
    }

    @RequestMapping("student-rejectOrder.do")
    public String studentRejectOrder(String orderFormID,HttpSession session){
        Student student = (Student)session.getAttribute("student");
        if(student == null){
            return "redirect:/";
        }
        String res = orderFormService.studentRejectOrder(student,orderFormID,session);
        System.out.println("res-studentRejectOrder: "+res);
        if(!res.equals("success")){
            return "404";
        }
        return "redirect:/studentGetAllMySellOrders.do";
    }

    @RequestMapping("student-cancelBuyOrder.do")
    public String studentCancelBuyOrder(String orderFormID,HttpSession session){
        Student student = (Student)session.getAttribute("student");
        if(student == null){
            return "redirect:/";
        }
        String res = orderFormService.studentCancelBuyOrder(student,orderFormID,session);
        System.out.println("res-studentCancelBuyOrder: "+res);
        if(!res.equals("success")){
            return "404";
        }

        return "redirect:/studentGetAllMyBuyOrders.do";
    }

    @RequestMapping("studentGetAllMySellOrders.do")
    public String studentGetAllMySellOrders(HttpSession session){
        Student student = (Student)session.getAttribute("student");
        if(student == null){
            return "redirect:/";
        }
        String res = orderFormService.studentGetAllMySellOrders(student,session);
        System.out.println("res-studentGetAllMyBuyOrders: "+res);
        if(!res.equals("success")){
            return "404";
        }

        return "redirect:/newStudent-seeAllMySellOrders";
    }

    @RequestMapping("studentGetAllMyBuyOrders.do")
    public String studentGetAllMyBuyOrders(HttpSession session){
        Student student = (Student)session.getAttribute("student");
        if(student == null){
            return "redirect:/";
        }
        String res = orderFormService.studentGetAllMyBuyOrders(student,session);
        System.out.println("res-studentGetAllMyBuyOrders: "+res);
        if(!res.equals("success")){
            return "404";
        }
        return "redirect:/newStudent-seeAllMyBuyOrders";
    }

    @ResponseBody
    @RequestMapping("student-submitOrder.do")
    public String studentSubmitOrder(String ebickID,String details , HttpSession session){
        Student student = (Student)session.getAttribute("student");
        if(student == null){
            return "请先登录";
        }
        OrderForm orderForm = new OrderForm();
        orderForm.setEbickID(ebickID);
        orderForm.setDetails(details);
        String res = orderFormService.studentSubmitOrder(student,orderForm,session);
        System.out.println("res-studentSubmitOrder: "+res);

        return res;
    }

    @RequestMapping("schoolCounsellor-getAllOrders.do")
    public String schoolCounsellorGetAllOrders(HttpSession session){
        SchoolCounsellor schoolCounsellor = (SchoolCounsellor) session.getAttribute("schoolCounsellor");
        if(schoolCounsellor==null)return "redirect:/";
        String res = orderFormService.schoolCounsellorGetAllOrders(schoolCounsellor,session);
        System.out.println("res-schoolCounsellorGetAllOrders: "+res);
        if(!res.equals("success")){
            return "404";
        }
        return "redirect:/schoolCounsellor-seeAllOrders";
    }

    @RequestMapping("admin-getAllOrders.do")
    public String adminGetAllOrders(HttpSession session){
        Admin admin = (Admin)session.getAttribute("admin");
        if(admin==null)return "redirect:/adminLogin";
        String res = orderFormService.adminGetAllOrders(admin,session);
        System.out.println("res-adminGetAllOrders: "+res);
        if(!res.equals("success")){
            return "404";
        }
        return "redirect:/admin-seeAllOrders";
    }

    @ResponseBody
    @RequestMapping("admin-deleteOneOrderForm.do")
    public String adminDeleteOneOrderForm(HttpSession session,String orderFormID){
        Admin admin = (Admin)session.getAttribute("admin");
        if(admin==null)return "请先登录";
        String res = orderFormService.adminDeleteOneOrderForm(admin,orderFormID,session);
        System.out.println("res-adminDeleteOneOrderForm: "+res);

        return res;
    }

}
